package org.jjche.sys.modules.fillrule.function;

import lombok.RequiredArgsConstructor;
import org.jjche.log.biz.service.IParseFunction;
import org.jjche.log.biz.service.impl.DiffParseFunction;
import org.jjche.log.context.LogRecordContext;
import org.jjche.sys.modules.fillrule.domain.SysFillRuleDO;
import org.jjche.sys.modules.fillrule.mapstruct.SysFillRuleMapStruct;
import org.jjche.sys.modules.fillrule.service.SysFillRuleService;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * 系统编码规则 设置修改/删除前的数据到变量
 * </p>
 *
 * @author miaoyj
 * @since 2024-05-11
 */
@Component
@RequiredArgsConstructor
public class SysFillRuleDiffOldByIdParseFunction implements IParseFunction {
    private final SysFillRuleService sysFillRuleService;
    private final SysFillRuleMapStruct sysFillRuleMapStruct;

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean executeBefore() {
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String functionName() {
        return "SYS_FILL_RULE_DIFF_OLD_BY_ID";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String apply(Object idObj) {
        Object result = null;
        if (idObj instanceof List) {
            List<Long> ids = (List<Long>) idObj;
            List<SysFillRuleDO> list = this.sysFillRuleService.listByIds(ids);
            result = this.sysFillRuleMapStruct.toDTO(list);
        } else {
            Long id = (Long) idObj;
            SysFillRuleDO sysFillRuleDO = this.sysFillRuleService.getById(id);
            result = this.sysFillRuleMapStruct.toDTO(sysFillRuleDO);
        }
        LogRecordContext.putVariable(DiffParseFunction.OLD_OBJECT, result);
        return null;
    }
}
